package com.ts.api.module.api.convert.receive.entity;

import com.gitee.apanlh.exp.HttpBodyParseException;
import com.gitee.apanlh.util.net.http.HttpBody;
import com.gitee.apanlh.util.net.http.HttpClient;
import com.gitee.apanlh.util.net.http.HttpRequest;
import com.gitee.apanlh.util.net.http.HttpRequestHeader;
import com.gitee.apanlh.util.net.http.handler.body.HttpBodyParseStrategy;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.common.redis.ApiMatchRedisKey;
import lombok.Data;

import java.util.Map;

/**
 *  进件通知主动发送业务实体类
 *
 *  @author Pan
 */
@Data
public class ApiReceiveNotice {

    /** 拓展url将会拼接在url后面 */
    private String extendUrl;
    /** 提交方式 */
    private HttpContentType httpContentType;
    /** 请求头 */
    private HttpRequestHeader httpRequestHeader;
    /** 关闭UrlEncode */
    private boolean disableUrlEncode;
    /** 机构id */
    private Long productId;
    /** 客户端id */
    private String apiClientId;
    /** 手机号md5 */
    private String phoneMd5;
    /** 唯一请求id */
    private String reqId;

    public void setDisableUrlEncode(boolean disableUrlEncode) {
        this.disableUrlEncode = disableUrlEncode;
    }

    public boolean getDisableUrlEncode() {
        return this.disableUrlEncode;
    }

    /**
     *  获取撞库时保存的唯一id(或者其他数据)
     *
     *  @return String
     */
    public String getDataExchangeId() {
        String key = ApiMatchRedisKey.getKey(this.reqId, this.phoneMd5);
        String value = ApiMatchRedisKey.API_CHANNEL_DATA_EXCHANGE_REQ_ID.get(key, String.class);
        return value;
    }

    /**
     *  默认构造函数
     */
    public ApiReceiveNotice() {
       super();
    }

    /**
     *  转化请求方式为NameValuePair格式(值对节点类型)
     *
     * 	@author Pan
     *  @param  paramMap 请求参数
     *  @return	String
     */
    public void appendExtendUrl(Map<String, Object> paramMap) {
        HttpBodyParseStrategy httpBodyParseStrategy = new HttpBodyParseStrategy() {
            @Override
            public <V> String toNameValuePair(Map<String, V> paramMap, boolean hasParamUrlEncode) {
                return HttpBodyParseStrategy.super.toNameValuePair(paramMap, hasParamUrlEncode);
            }

            @Override
            public Map<String, ?> beanToLinkedMap(HttpBody httpBody) {
                return HttpBodyParseStrategy.super.beanToLinkedMap(httpBody);
            }

            @Override
            public HttpRequest parse(HttpRequest httpRequest, HttpClient httpClient) throws HttpBodyParseException {
                return null;
            }
        };
        String queryParam = httpBodyParseStrategy.toNameValuePair(paramMap, this.disableUrlEncode);
        this.setExtendUrl("?" + queryParam);
    }
}
